//@author A0086393U
package rightnow.ui.widgets;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javafx.scene.layout.VBox;
import rightnow.logic.FeedbackProvider;
import rightnow.storage.Task;
import rightnow.ui.widgets.TaskController;

/**
 * This class is a mock list controller used in the FeedbackProviderTest,
 * so FXML is not required to be called.
 *
 */
public class MockListController implements Observer {		

	VBox listVBox;
	
	private ArrayList<TaskController> tasksInPane = new ArrayList<TaskController>();
	private TaskController emptyPane;
	protected double tasksHeight;
	
	public MockListController() {
		// initialize listVBox
		listVBox = new VBox();
		
		// Create the empty pane
		emptyPane = new TaskController();

		// Use an empty task list to initialize the list view
		ArrayList<Task> emptyTaskList = new ArrayList<Task>();
		listVBox.getChildren().setAll(createTaskViews(emptyTaskList));
		
		FeedbackProvider.getInstance().addObserver(this);
	}
	
	
	public void updateTaskView(List<Task> tasks) {
		listVBox.getChildren().setAll(createTaskViews(tasks));
	}
	
	public void updateSingleTask(Task task, int atIndex) {
		TaskController taskCont = new TaskController(task, atIndex, true);
		listVBox.getChildren().set(atIndex, taskCont);
	}
	
	private ArrayList<TaskController> createTaskViews(List<Task> tasksToList) {
		// if tasksToList is empty, display the emptyPane
		if (tasksToList.isEmpty()) {
			tasksInPane.add(emptyPane);
			return tasksInPane;
		}
		
		tasksInPane = new ArrayList<TaskController>();
		tasksHeight = 0.0f;
		
		// Create a pane for each available task and add it to the tasksInPane.
		// Keep track of the index for UI purposes, as the index will serve as a
		// way to specify which tasks to operate upon.
		int index = 0;
		for (Task t : tasksToList) {
			TaskController taskPane = new TaskController(t, index, true);
			tasksInPane.add(taskPane);
			tasksHeight += taskPane.getHeight();
			index++;
		}
		
		return tasksInPane;
	}

	/* Getters and setters */
	
	public VBox getListVBox() {
		return listVBox;
	}

	public void setListVBox(VBox listVBox) {
		this.listVBox = listVBox;
	}


	@Override
	public void update(Observable obs, Object arg) {
		if (obs == FeedbackProvider.getInstance()) {
			FeedbackProvider fp = FeedbackProvider.getInstance();
			if (fp.hasListChanged()) {
				updateTaskView(fp.getTasksForUi());
			}
		}
	}
	
}
